package com.ly.honor.controller;



import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.honor.entity.Player;
import com.ly.honor.entity.dto.PlayerCreateDTO;
import com.ly.honor.entity.dto.PlayerEditDTO;
import com.ly.honor.entity.dto.PlayerQueryDTO;
import com.ly.honor.entity.vo.PlayerQueryVO;
import com.ly.honor.service.PlayerService;
import com.ly.honor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * (Player)表控制层
 *
 * @author makejava
 * @since 2022-11-16 11:53:59
 */
@RestController
@RequestMapping("player")
@Api(tags = "选手管理")
public class PlayerController {
    /**
     * 服务对象
     */
    @Resource
    private PlayerService playerService;

    /**
     * 分页查询所有数据
     * @param pageNum
     * @param pageSize
     * @param playerQueryDTO
     * @return
     */
    @ApiOperation("分页查询所有数据")
    @PostMapping("selectPlayerPage/{pageNum}/{pageSize}")
    public Result selectPlayerPage(@PathVariable("pageNum") long pageNum,
                                   @PathVariable("pageSize") long pageSize,
                                   @RequestBody @Valid PlayerQueryDTO playerQueryDTO) {
        Page<PlayerQueryVO> playerQueryVOPage = playerService.selectPlayerPage(pageNum,pageSize,playerQueryDTO);
        return Result.ok(playerQueryVOPage);
    }

    @ApiOperation("根据条件查询所有选手")
    @PostMapping("getPlayerListByCondition")
    public Result getPlayerListByCondition(@RequestBody PlayerQueryDTO playerQueryDTO) {
        List<PlayerQueryVO> playerQueryVOList = playerService.getPlayerListByCondition(playerQueryDTO);
        return Result.ok(playerQueryVOList);
    }

    @ApiOperation("查询所有选手")
    @GetMapping("selectPlayerAll")
    public Result selectPlayerAll() {
        return Result.ok(playerService.list());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("getPlayerById/{id}")
    public Result getPlayerById(@PathVariable long id) {
        PlayerQueryVO playerQueryVO = playerService.getPlayerById(id);
        return Result.ok(playerQueryVO);
    }

    /**
     * 新增数据
     *
     * @param playerCreateDTO 实体对象
     * @return 新增结果
     */
    @ApiOperation("新增选手")
    @PostMapping("insertPlayer")
    public Result insertPlayer(@RequestBody @Valid PlayerCreateDTO playerCreateDTO) {
        playerService.insertPlayer(playerCreateDTO);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param playerEditDTO 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改选手")
    @PostMapping ("updatePlayer")
    public Result updatePlayer(@RequestBody @Valid PlayerEditDTO playerEditDTO) {
        playerService.updatePlayer(playerEditDTO);
        return Result.ok();
    }

    @ApiOperation("指定选手退役")
    @PostMapping ("retirePlayer")
    public Result retirePlayer(@RequestBody List<Long> idList) {
        playerService.retirePlayer(idList);
        return Result.ok();
    }

    @ApiOperation("指定选手重新复出")
    @PostMapping ("comebackPlayer")
    public Result comebackPlayer(@RequestBody List<Long> idList) {
        playerService.comebackPlayer(idList);
        return Result.ok();
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @ApiOperation("通过主键删除单条信息")
    @DeleteMapping("deletePlayerById/{id}")
    public Result deletePlayerById(@PathVariable("id") long id) {
        playerService.deletePlayerById(id);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation("批量删除")
    @DeleteMapping("removeBatch")
    public Result removeBatch(@RequestBody List<Long> idList) {
        playerService.removeBatch(idList);
        return Result.ok();
    }

    @PostMapping("upload")
    @ApiOperation("文件上传")
    public Result uploadFile(MultipartFile file){
        String url = playerService.upload(file);
        return Result.ok(url).message("上传文件成功");
    }

    @ApiOperation("导出现役选手为excel")
    @GetMapping("exportNowPlayerExcel")
    public void exportNowPlayerExcel(HttpServletResponse response){
        playerService.exportNowPlayerExcel(response);
    }

    @ApiOperation("导入现役选手")
    @PostMapping("importNowPlayerExcel")
    public Result importNowPlayerExcel(MultipartFile file){
        playerService.importNowPlayerExcel(file);
        return Result.ok();
    }

    @ApiOperation("导出退役选手为excel")
    @GetMapping("exportRetirePlayerExcel")
    public void exportRetirePlayerExcel(HttpServletResponse response){
        playerService.exportRetirePlayerExcel(response);
    }

    @ApiOperation("获取选手分布情况")
    @GetMapping("getPlayerSpread")
    public Result getPlayerSpread(){
        List<JSONObject> playerSpreadList =  playerService.getPlayerSpread();
        return Result.ok(playerSpreadList);
    }

    @ApiOperation("查看所有现役选手")
    @GetMapping("getNowPlayerList")
    public Result getNowPlayerList(){
        List<PlayerQueryVO> playerList =  playerService.getNowPlayerList();
        return Result.ok(playerList);
    }

    @ApiOperation("查看所有退役选手")
    @GetMapping("getRetirePlayerList")
    public Result getRetirePlayerList(){
        List<PlayerQueryVO> playerList =  playerService.getRetirePlayerList();
        return Result.ok(playerList);
    }
}

